package com.mrjy.maintain.bo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.mrjy.common.dto.MenuDto;
import com.mrjy.common.entity.MenuInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.maintain.dao.IMenuInfoDao;
import com.mrjy.maintain.dao.ISchoolInfoDao;
import com.mrjy.maintain.dto.SchoolInfoDTO;
import com.mrjy.maintain.util.RoleInfoEnum;

@Service("menuInfoBo")
public class MenuInfoBo implements IMenuInfoBo{
	@Resource
	private IMenuInfoDao menuInfoDao;
	@Resource
	private ISchoolInfoDao schoolInfoDao;
	
	@Override
	public Map<String, Object> queryMenuByRoleId(UserInfo info) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == info){
			map.put("err_code", 4001);
			map.put("err_msg", "用户信息不存在");
			return map;
		}
		if(null == info.getRoleId()){
			map.put("err_code", 4002);
			map.put("err_msg", "用户角色ID不存在");
			return map;
		}
		
		List<MenuInfo> menuInfos = menuInfoDao.queryMenuByRoleId(info.getRoleId());
		//根据学校的id
		SchoolInfoDTO dto = null;
		if(info.getSchoolId() != null && (info.getRoleId() == RoleInfoEnum.SCHOOL_ADMIN.getRoleId() || info.getRoleId() == RoleInfoEnum.SERVICE_SCHOOL_ADMIN.getRoleId())){
			dto = schoolInfoDao.queryBySchoolId(info.getSchoolId());
		}
		List<MenuDto> menuList = new ArrayList<MenuDto>();
		for(MenuInfo menuInfo : menuInfos){
			if(menuInfo.getMenuLevel().intValue()==1){
				MenuDto menuDto = new MenuDto();
				menuDto.setMenuid(menuInfo.getId().toString());
				menuDto.setComponent(upperString(menuInfo.getMenuUrl()));
				menuDto.setName(menuInfo.getMenuName());
				menuDto.setPath(menuInfo.getMenuUrl());
				menuDto.setIcon(menuInfo.getMenuIcon());
				menuDto.setMenuCode(menuInfo.getMenuCode());
				List<MenuDto> mList = new ArrayList<MenuDto>();
				for(MenuInfo menu : menuInfos){
					if(menu.getSuperMenu().equals(menuInfo.getMenuCode())){
						MenuDto mDto = new MenuDto();
						if(menu.getId() != 406){
							mDto.setMenuid(menu.getId().toString());
							mDto.setComponent(upperString(menu.getMenuUrl()));
							mDto.setName(menu.getMenuName());
							mDto.setPath("admin/"+menu.getMenuUrl());//加admin权限拦截
							mDto.setIcon(menu.getMenuIcon());
							mDto.setMenuCode(menu.getMenuCode());
							mList.add(mDto);
						}else{
							if(info.getRoleId() == RoleInfoEnum.SCHOOL_ADMIN.getRoleId() || info.getRoleId() == RoleInfoEnum.SERVICE_SCHOOL_ADMIN.getRoleId() || info.getRoleId() == RoleInfoEnum.SCHOOL_ADMIN.getRoleId() ){
								if(null != dto && 1== (dto.getIsDiy())){//diy专区
									mDto.setMenuid(menu.getId().toString());
									mDto.setComponent(upperString(menu.getMenuUrl()));
									mDto.setName(menu.getMenuName());
									mDto.setPath("admin/"+menu.getMenuUrl());//加admin权限拦截
									mDto.setIcon(menu.getMenuIcon());
									mDto.setMenuCode(menu.getMenuCode());
									mList.add(mDto);
								}
							}else {
								mDto.setMenuid(menu.getId().toString());
								mDto.setComponent(upperString(menu.getMenuUrl()));
								mDto.setName(menu.getMenuName());
								mDto.setPath("admin/"+menu.getMenuUrl());//加admin权限拦截
								mDto.setIcon(menu.getMenuIcon());
								mDto.setMenuCode(menu.getMenuCode());
								mList.add(mDto);
							}
							
						}
					}
				}
				Collections.sort(mList);
				menuDto.setMenus(mList);
				menuList.add(menuDto);
			}
		}
		
		Collections.sort(menuList);
		
		if(null != menuList){
			map.put("err_code", "200");
			map.put("err_msg", "菜单查询成功");
			map.put("menus", menuList);
		}
		return map;
	}
	
	/**
	 * 
	 * @author Javin
	 * @date 2017年8月31日
	 * @desc 第一个字母大写
	 * @param str
	 * @return
	 */
	public String upperString(String str){
		if(StringUtils.isNotBlank(str)){
			return str.substring(0,1).toUpperCase() + str.substring(1);
		}
		return str;
	}

	@Override
	public Object queryByMenuUrl(String menuUrl) {
		return menuInfoDao.queryByMenuUrl(menuUrl);
	}

	@Override
	public List<MenuDto> queryAllMenu() {
		List<MenuInfo> menulist = menuInfoDao.queryAllMenu();
		List<MenuDto> result = new ArrayList<MenuDto>();
		//封装数据
		for (MenuInfo menuInfo : menulist) {
			if (menuInfo.getMenuLevel()==1) {  //如果是1级菜单
				MenuDto menuDto = new MenuDto();
				menuDto.setMenuCode(menuInfo.getMenuCode());
				menuDto.setMenuid(menuInfo.getId()+"");
				menuDto.setName(menuInfo.getMenuName());
				List<MenuDto> mList = new ArrayList<MenuDto>();
				for (MenuInfo menuInfo2 : menulist) {
					if (menuInfo2.getSuperMenu().equals(menuInfo.getMenuCode())) {  //如果是该1级菜单的子菜单
						MenuDto mDto = new MenuDto();
						mDto.setMenuCode(menuInfo2.getMenuCode());
						mDto.setMenuid(menuInfo2.getId()+"");
						mDto.setName(menuInfo2.getMenuName());
						mList.add(mDto);
					}
				}
				menuDto.setMenus(mList);
				result.add(menuDto);
			}
			
		}
		return result;
	}	
}
